import pandas as pd
import tushare as ts
# pd.set_option('display.max_columns', None)
# pd.set_option('display.max_rows', None)
import __init__
import sys
import other
from get_data.tusharecopy.stock import wencaicopy
import numpy as np
import re
from get_data import Altas_db

start_time, today_time, _hour_ = other.time_start(
    dtype="with-")  # start_time,today_time,today_hour=时间('20200731', '20200820', '16:12')

n_start_time, n_today_time, n_hour_ = other.time_start(days=15, )
# print(start_time, today_time)
# other.funcat_time()
# ————————————————————————————————————————————————————————————
"""
df1
当日数据get_today_ts()


df2
    df2
        历史数据get_his_data
        历史数据 for循环 getHisData_main
    df2fast
df3
当日数据get_today_data_else()
3.1 及时数据处理 
*****
ts_his
    df1 vol是股数，
    df2 vol是手
    df3是根据df2，是手
ths_vol单位是万手
"""
# df1————————————————————————————————————————————————————————————
@other.time_
def get_today_ts():
    try:
        csv_data = ts.get_today_all()  # 读取today实时数据
        csv_data["liutongliang"] = csv_data["nmc"] / csv_data["trade"]
        # csv_data.to_csv("db_temp/1.数据下载.txt", encoding="utf-8")
        # print("保存数据到db_temp/1.数据下载.txt中,获取{}项".format(len(csv_data)))
        csv_data["volume"]=csv_data["volume"]/100
        Altas_db._save_mongo_db(csv_data, "ts", "1.数据下载")

    except:
        print("从db_temp/1.数据下载.txt中，读取数据")
        csv_data = pd.read_csv("db_temp/1.数据下载.txt", index_col=0)
    csv_data = other.re_read_df(csv_data)
    # else:
    #     csv_data=get_today_data_else()
    return csv_data
# df2————————————————————————————————————————————————————————————
def get_his_data(code='600848', num=3, start=start_time, end=today_time):
    """

    p_change：涨跌幅
ma5：5日均价
ma10：10日均价
ma20:20日均价
v_ma5:5日均量
v_ma10:10日均量
v_ma20:20日均量
turnover:换手率[注：指数无此项]
                 high    low  close    volume
date
2021-11-09  14.30  14.18  14.23  21198.71
2021-11-08  14.27  14.16  14.24  23478.14
('600848', 14.3, 14.18, 14.23, 21198.71, 14.27, 14.16, 14.24, 23478.14)
#('600848', 2021-11-09 , 2021-11-08 )
    :param code:
    :return:
    """
    other._write_console()
    df = ts.get_hist_data(code, start=start, end=end)  # 一次性获取全部日k线数据
    # print(df)
    try:
        if len(df) == 0:
            return ()
        df = df[["high", "low", "close", "volume"]][:num]
        # print(df)
        df = df.apply(lambda x: tuple(x), axis=1).values.tolist()
        stock_tuple = (code,) + df[0] + df[1] + df[2]
        # print(stock_tuple)
        if len(stock_tuple) == 4 * num + 1:
            return stock_tuple
        else:
            # 对应none数据
            return ()
    except:
        return ()
@other.time_
def getHisData_main():
    """
    从db_temp/1.数据下载.txt获取数据->stock_list
    for循环i_stock
        可视化进度条
    +时间
    +保存

    >>>结果返回今天及前2天数据
    ,code,h_,l_,c_,v_,h_1,l_1,c_1,v_1,h_2,l_2,c_2,v_2,time
0,688981,54.89,54.3,54.71,223466.41,55.33,54.52,54.57,237462.19,55.71,54.61,55.26,231262.47,20211119
1,688819,48.28,45.11,47.74,77913.51,46.5,44.5,45.66,65665.07,44.56,42.82,44.44,38836.96,20211119
    975秒，约16分钟。
    :return:
    """
    # stock_list=other.stock_all_list(path_="db_temp/1.数据下载.txt")[:]#["002274","300789"]
    stock_list = other.stock_all_list("ts", "1.数据下载")[:]
    _stock_df = []
    i = 0
    print(stock_list, len(stock_list))
    for i_stock in stock_list:
        stock_tuple = get_his_data(code=i_stock)
        _stock_df.append(stock_tuple)
        # 可视化
        i += 1
        other.progress_bar(i)
    stock_df = pd.DataFrame(_stock_df,
                            columns=['code', 'h_', 'l_', 'c_', 'v_', 'h_1', 'l_1', 'c_1', 'v_1', 'h_2', 'l_2', 'c_2',
                                     'v_2'])
    stock_df["time"] = other.funcat_time()[0]
    # print(stock_df)
    Altas_db._save_mongo_db(stock_df, "ts", "2.his数据下载")
    # stock_df.to_csv("db_temp/2.his数据下载.txt", encoding="utf-8")
    return stock_df
def time_front30_data():
    day_time=other.time_saveandget(choose="day_list",day=50)
    if len(day_time)>=35:
        #print("1",len(day_time))
        pass

    if len(day_time)<35:
        day_time = other.time_saveandget(choose="day_list", day=70)
        if len(day_time)>=35:
            #print("2", len(day_time))
            pass

        if len(day_time)<35:
            print("3", len(day_time))
            day_time = other.time_saveandget(choose="day_list", day=100)
    day_time=sorted(day_time,reverse=True)#降序排列
    day_time_front30=day_time[:]
    # print(day_time_front30,len(day_time_front30))#['20211203', '20211202', '20211201', '20211130', '20211129', '20211126', '20211125', '20211124', '20211123', '20211122', '20211119', '20211118', '20211117', '20211116', '20211115', '20211112', '20211111', '20211110', '20211109', '20211108', '20211105', '20211104', '20211103', '20211102', '20211101', '20211029', '20211028', '20211027', '20211026', '20211025'] 30
    day_time_start=day_time_front30[-1]
    return day_time_start
@other.time_
def get_his_data_fast(model="short"):
    """
    >>>作为getHisData_main fast版本,多股票运行,效率更快,约20秒.
     从pro.stock_basic(->len_data
     for循环len_data
        [000001.SZ,000002.SZ,000004.SZ,000005.SZ,000006.SZ,000007.SZ,000008.SZ...]
        list变为str
         pro.daily(ts_code=

     +保存

     >>>结果返回今天及前2天数据
     """

    if model=="short":
        global n_start_time
        global n_today_time
        pass
    if model=="long":
        n_start_time=time_front30_data()
        print("::::::",n_start_time,n_today_time)

    pro = ts.pro_api()
    df = pro.stock_basic(exchange='', list_status='L', fields='')
    len_data = int(len(df) / 100 + 1)
    temp = pd.DataFrame()

    for i in range(len_data):
        ts_get_datalist = df[100 * i:100 * (i + 1)].ts_code.tolist()
        # print(",".join(ts_get_datalist))
        test_list = ",".join(ts_get_datalist)

        # 多个股票
        #   df = pro.daily(ts_code='000001.SZ,600000.SH', start_date='20180701', end_date='20180718')
        _df = pro.daily(ts_code=test_list, start_date=n_start_time, end_date=n_today_time)
        temp = pd.concat([temp, _df])
    temp = pd.concat([temp,
                      temp.ts_code.str.split('.', expand=True).rename(columns={0: 'code', 1: 'sz代码'})], axis=1)
    if model=="short":
        Altas_db._save_mongo_db(temp, "ts", "2.his数据下载_fast1")
        temp = _df_handle_method(temp)
        # temp.to_csv("db_temp/2.his数据下载_fast.txt", encoding="utf-8")
        Altas_db._save_mongo_db(temp, "ts", "2.his数据下载_fast")
    if model=="long":
        Altas_db._save_mongo_db(temp, "ts", "2.his数据下载_fast1_long")
        temp = _df_handle_method(temp)
        # temp.to_csv("db_temp/2.his数据下载_fast.txt", encoding="utf-8")
        Altas_db._save_mongo_db(temp, "ts", "2.his数据下载_fast_long")


    return temp
def _df_handle_method(df):
    n = list(set(df.trade_date))
    # 用sort将列表进行排序，reverse=True为降序设置
    n.sort(reverse=True)
    if len(n) > 2:
        # print(df[df.trade_date == n[0]])
        df_1 = df[df.trade_date == n[0]][['ts_code', "high", "low", "close", "vol"]]
        df_2 = df[df.trade_date == n[1]][['ts_code', "high", "low", "close", "vol"]]
        df_3 = df[df.trade_date == n[2]][['ts_code', "high", "low", "close", "vol"]]
        _df = pd.merge(df_1, df_2, how='inner', on='ts_code')
        _df = pd.merge(_df, df_3, how='inner', on='ts_code')

        _df.rename(
            columns={'ts_code': "code", "high_x": "h_", "low_x": "l_", "close_x": "c_", "vol_x": "v_", "high_y": "h_1",
                     "low_y": "l_1", "close_y": "c_1", "vol_y": "v_1", "high": "h_2", "low": "l_2", "close": "c_2",
                     "vol": "v_2"}, inplace=True)
        _df = pd.concat([_df[['h_', 'l_', 'c_', 'v_', 'h_1', 'l_1', 'c_1', 'v_1', 'h_2', 'l_2', 'c_2', 'v_2']],
                         _df.code.str.split('.', expand=True).rename(columns={0: 'code', 1: 'sz代码'})],
                        axis=1)  # 将000001.SZ 进行列拆分
        del _df['sz代码']
        _df["time"] = n[0]
        _df = pd.DataFrame(_df,
                           columns=["code", 'h_', 'l_', 'c_', 'v_', 'h_1', 'l_1', 'c_1', 'v_1', 'h_2', 'l_2', 'c_2',
                                    'v_2', "time"])
        return _df

    else:
        raise ("列表日期少于3天")
# df3————————————————————————————————————————————————————————————
@other.time_
def get_today_data_else():
    """
    时常3s
    1.读取股票dataframe（补全）
    2.for循环code
    :return:
    """
    # df = pd.read_csv("db_temp/1.数据下载.txt", encoding="utf-8", index_col=0)
    df = Altas_db._readdf('ts', '1.数据下载')
    df = other.re_read_df(df)
    len_data = int(len(df) / 450 + 1)
    temp = pd.DataFrame()

    for i in range(len_data):
        ts_get_datalist = df[450 * i:450 * (i + 1)].code.tolist()
        _df = ts.get_realtime_quotes(ts_get_datalist)  # Single stock symbol
        _df = _df[['code', 'open', 'price', 'low', 'high', 'pre_close', 'volume', 'time']]

        temp = pd.concat([temp, _df])

    ts_get_data = temp.reset_index(drop=True)  # 重建索引
    # obj格式变成float
    ts_get_data['open'] = pd.to_numeric(ts_get_data['open'])
    ts_get_data['low'] = pd.to_numeric(ts_get_data['low'])
    ts_get_data['trade'] = pd.to_numeric(ts_get_data['price'])
    ts_get_data['high'] = pd.to_numeric(ts_get_data['high'])
    ts_get_data['volume'] = pd.to_numeric(ts_get_data['volume'])
    ts_get_data['settlement'] = pd.to_numeric(ts_get_data['pre_close'])
    ts_get_data["volume"] = ts_get_data["volume"] / 100
    # print("df_3", ts_get_data)
    # ts_get_data.to_csv("db_temp/1.1.数据备份快速下载.txt", encoding="utf-8")
    Altas_db._save_mongo_db(ts_get_data, "ts", "1.1.数据备份快速下载")
    return ts_get_data

def df2Mergedf1():
    """
    1.读取历史股票df_2
     1.1逻辑：
        if 时间相等：
            if周六：
                2.his数据下载
            else:非周六
                3.数据+2.his数据下载
        else:
            重新运行2.his数据下载
            df=重新运行程序
            return df

    """
    df_2 = other.stock_all_list("ts", "2.his数据下载", dtype="2.1")
    a1 = (other.time_zhou6_0(dtype="df").pretrade_date[-1:]).item()
    # print(df_2)
    a2 = df_2.time[0]
    print(a1, a2)
    if (str(a1) == str(a2)) or (str(a1) < str(a2)):  # 日期相等,a1是ts时间会小于a2获取时间
        if int(other.time_zhou6_0(dtype="df")[-1:].is_open) == 0:  # 周六
            # df_2.to_csv("db_temp/3.1数据.txt", encoding="utf-8",index=False)
            # print("保存数据到db_temp/3.1数据.txt中,获取{}项".format(len(df_2)))
            Altas_db._save_mongo_db(df_2, "ts", "3.1数据")
            return df_2
        else:  # 平时
            if _hour_>"15:30":
                dtype = "2"
            if _hour_<"15:30":
                dtype = "1"
            if dtype == "2":
                df_3 = get_today_data_else()

                _df = pd.merge(df_2, df_3, how='inner', on='code')
            else:
                df_1 = get_today_ts()
                _df = pd.merge(df_2, df_1, how='inner', on='code')
                _df = _method_mm(_df)  # 数据整理
            # _df.to_csv("db_temp/3.1数据.txt", encoding="utf-8")
            Altas_db._save_mongo_db(_df, "ts", "3.1数据")
            return _df
    else:
        print("日期不等,重新获取数据,重新运行df_2")
        stock_df = get_his_data_fast()  # 代替，从30分钟降到15秒->getHisData_main()
        # stock_df.to_csv("db_temp/2.his数据下载.txt", encoding="utf-8")
        Altas_db._save_mongo_db(stock_df, "ts", "2.his数据下载")
        _df = df2Mergedf1()
        return _df


def _method_mm(df):
    df["pre_close"] = df["settlement"]
    df["price"] = df["trade"]
    df["time_y"] = ""
    df = df[["code", 'h_', 'l_', 'c_', 'v_', 'h_1', 'l_1', 'c_1', 'v_1', 'h_2', 'l_2', 'c_2', 'v_2', "time",
             "open", "price", "low", "high", "pre_close", "volume", "time_y", "trade", "settlement",
             "turnoverratio", "amount", "per", "pb", "mktcap", "nmc", "liutongliang"]]
    return df


# di1————————————————————————————————————————————————————————————
def DMI_method_60():  # 周六
    df = df2Mergedf1()

    # df = df.loc[:, ~df.columns.str.contains('^Unnamed')].drop_duplicates()  # 2去掉Unnamed 并且去重

    # 1.1 TR
    # df=df.set_index('code',drop=False)
    a = np.array(df)
    # print(a.shape)
    # ['code','h_','l_','c_','v_','h_1','l_1','c_1','v_1','h_2','l_2','c_2','v_2'])
    # a=a[:,1]-a[:,2]
    a1 = (a[:, 1] - a[:, 2])
    a2 = abs(a[:, 1] - a[:, 7])
    a3 = np.max([a1, a2], axis=0)
    a4 = abs(a[:, 2] - a[:, 7])
    a5 = np.max([a3, a4], axis=0)

    a6 = (a[:, 5] - a[:, 6])
    a7 = abs(a[:, 5] - a[:, 11])
    a8 = np.max([a6, a7], axis=0)
    a9 = abs(a[:, 6] - a[:, 11])
    a10 = np.max([a8, a9], axis=0)
    # print(a5.shape , a10.shape)
    tr = a5 + a10
    # 1.2 HD
    hd1 = (a[:, 1] - a[:, 5])
    ld1 = (a[:, 6] - a[:, 2])
    # 1.3 DMP
    # 假设我们想要根据cond中的值选取xarr和yarr的值：当cond中的值为true时，选取xarr的值，否则从yarr中选取。列表推导式的写法应该如下所示：
    # xarr = np.array([1.1,1.2,1.3,1.4,1.5])
    # yarr = np.array([2.1,2.2,2.3,2.4,2.5])
    # cond = np.array([True,False,True,True,False])
    # result = [(x if c else y) for x, y, c in zip(xarr, yarr, cond)]
    # print(result)
    cond1 = (hd1 > 0) & (hd1 > ld1)
    xarr1 = hd1
    yarr1 = np.zeros((len(xarr1),), dtype=int)
    result1 = [(x if c else y) for x, y, c in zip(xarr1, yarr1, cond1)]
    dmp1 = np.array(result1)

    hd2 = (a[:, 5] - a[:, 9])
    ld2 = (a[:, 10] - a[:, 6])
    cond2 = (hd2 > 0) & (hd2 > ld2)
    xarr2 = hd2
    yarr2 = np.zeros((len(xarr2),), dtype=int)
    result2 = [(x if c else y) for x, y, c in zip(xarr2, yarr2, cond2)]
    dmp2 = np.array(result2)

    dmp = dmp1 + dmp2
    df["di1"] = dmp / tr * 100

    # print(len(df[df.di1>45]))
    # df_=df[(df.h_>df.h_2)&(df.di1>45)]
    df_ = df
    # df_.sort_values(by="turnoverratio", ascending=False)
    # df_.to_csv("db_temp/3.2数据di1.txt", encoding="utf-8")
    Altas_db._save_mongo_db(df_, "ts", "3.2数据di1")
    # print("及时数据，包含停牌股票",len(df_))
    return df_


def DMI_method_61(df=""):  # 平时
    df = df2Mergedf1()

    # df = df.loc[:, ~df.columns.str.contains('^Unnamed')].drop_duplicates()  # 2去掉Unnamed 并且去重
    # df_3 =ts_his.get_today_data_else()
    # df = pd.merge(df, df_3, how='inner', on='code')
    #
    # df.to_csv("db_temp/3.1数据.txt", encoding="utf-8")
    # 1.1 TR
    # df=df.set_index('code',drop=False)
    a = np.array(df)
    # print(a.shape)
    # ['code','h_','l_','c_','v_','h_1','l_1','c_1','v_1','h_2','l_2','c_2','v_2'])
    # a=a[:,1]-a[:,2]
    a1 = (a[:, 1] - a[:, 2])
    a2 = abs(a[:, 1] - a[:, 7])
    a3 = np.max([a1, a2], axis=0)
    a4 = abs(a[:, 2] - a[:, 7])
    a5 = np.max([a3, a4], axis=0)
    """
    DMI_method_60代码成熟，采用a1-a5不变，a6-a10修改
    """
    a6 = (a[:, 17] - a[:, 16])
    a7 = abs(a[:, 17] - a[:, 22])
    a8 = np.max([a6, a7], axis=0)
    a9 = abs(a[:, 16] - a[:, 22])
    a10 = np.max([a8, a9], axis=0)
    # print(a5.shape , a10.shape)
    tr = a5 + a10
    # 1.2 HD
    hd1 = (a[:, 1] - a[:, 5])
    ld1 = (a[:, 6] - a[:, 2])
    # 1.3 DMP
    # 假设我们想要根据cond中的值选取xarr和yarr的值：当cond中的值为true时，选取xarr的值，否则从yarr中选取。列表推导式的写法应该如下所示：
    # xarr = np.array([1.1,1.2,1.3,1.4,1.5])
    # yarr = np.array([2.1,2.2,2.3,2.4,2.5])
    # cond = np.array([True,False,True,True,False])
    # result = [(x if c else y) for x, y, c in zip(xarr, yarr, cond)]
    # print(result)
    cond1 = (hd1 > 0) & (hd1 > ld1)
    xarr1 = hd1
    yarr1 = np.zeros((len(xarr1),), dtype=int)
    result1 = [(x if c else y) for x, y, c in zip(xarr1, yarr1, cond1)]
    dmp1 = np.array(result1)
    """
    DMI_method_60代码成熟，采用hd1不变，hd2修改
    """
    hd2 = (a[:, 17] - a[:, 1])
    ld2 = (a[:, 2] - a[:, 16])
    cond2 = (hd2 > 0) & (hd2 > ld2)
    xarr2 = hd2
    yarr2 = np.zeros((len(xarr2),), dtype=int)
    result2 = [(x if c else y) for x, y, c in zip(xarr2, yarr2, cond2)]
    dmp2 = np.array(result2)

    dmp = dmp1 + dmp2
    df["di1"] = dmp / tr * 100

    # print(len(df[df.di1>45]))
    # df_=df[(df.h_>df.h_2)&(df.di1>45)]
    df_ = df
    # df_.sort_values(by="turnoverratio", ascending=False)
    # df_.to_csv("db_temp/3.2数据di1.txt", encoding="utf-8")
    Altas_db._save_mongo_db(df_, "ts", "3.2数据di1")
    # print("及时数据，包含停牌股票",len(df_))
    return df_


# def DMI_1(M1=2, M2=1):
#     """
#     DMI 趋向指标
#     """
#     TR = SUM(MAX(MAX(HIGH - LOW, ABS(HIGH - REF(CLOSE, 1))), ABS(LOW - REF(CLOSE, 1))), M1)
#     HD = HIGH - REF(HIGH, 1)
#     LD = REF(LOW, 1) - LOW
#
#     DMP = SUM(IF((HD > 0) & (HD > LD), HD, 0), M1)
#     DMM = SUM(IF((LD > 0) & (LD > HD), LD, 0), M1)
#     DI1 = DMP * 100 / TR
#     DI2 = DMM * 100 / TR
#     ADX = MA(ABS(DI2 - DI1) / (DI1 + DI2) * 100, M2)
#     ADXR = (ADX + REF(ADX, M2)) / 2
# v_ma_5————————————————————————————————————————————————————————————
def his_fast1_mdTOma5():
    # df= pd.read_csv("db_temp/2.his数据下载_fast1.txt", index_col=0)
    df = Altas_db._readdf("ts", "2.his数据下载_fast1")

    n = list(set(df.trade_date))
    # 用sort将列表进行排序，reverse=True为降序设置
    n.sort(reverse=True)
    if len(n) >= 5:
        temp = df[df.trade_date == n[0]][['ts_code', "high", "low", "close", "vol"]]
        for i in range(1, 5):
            _df = df[df.trade_date == n[i]][['ts_code', "high", "low", "close", "vol"]]
            temp = pd.merge(temp, _df, how='inner', on='ts_code')
            # 计算v_ma_5
        temp.columns = [str(i) for i in range(21)]
    if len(n) < 5:
        print("请检查df,df出错误")
    a = np.array(temp)
    a_v_total = a[:, 4] + a[:, 8] + a[:, 12] + a[:, 16] + a[:, 20]
    a_v_ma5 = a_v_total / 5
    temp["v_ma5"] = a_v_ma5
    Altas_db._save_mongo_db(temp, "ts", "3.3数据v_ma5")
    temp = pd.concat([temp["v_ma5"], temp["0"].str.split('.', expand=True).rename(columns={0: 'code', 1: 'sz代码'})],
                     axis=1)  # 将000001.SZ 进行列拆分
    # Altas_db._save_mongo_db(temp,"ts","2.his数据下载_fast1_1")
    # temp.to_csv("db_temp/2.his数据下载_fast1_1.txt", encoding="utf-8")
    return temp[["code", "v_ma5"]]


def DmiWith_V_ma5():
    if int(other.time_zhou6_0(dtype="df")[-1:].is_open) == 0:  # 周六
        df_dmi1 = DMI_method_60()
    if int(other.time_zhou6_0(dtype="df")[-1:].is_open) == 1:  # 平时

        if _hour_ > "15:30":
            df_dmi1 = DMI_method_60()
        else:
            df_dmi1 = DMI_method_61()
    df_v_ma5 = his_fast1_mdTOma5()
    _df = pd.merge(df_dmi1, df_v_ma5, how='inner', on='code')
    # _df.to_csv("db_temp/3.4数据di1&v_ma5.txt", encoding="utf-8")
    Altas_db._save_mongo_db(_df, "ts", "3.4数据di1&v_ma5")
    return _df

#___________________________________
def Atr_get_his_fast(num=30):
    """
    编写思路：
    类似_df_handle_method
    类似DMI_method_61
    1.内容
    #(130000,13)
    # (4476,150)#时间近到远
    #data = pd.DataFrame(list_sum)[::-1]  # （30，4476）-》从上到下
    #MTR_data ATR   np.maximum(MTR_data, ATR)
    # price_close=(4470,29)->(29,4470) 倒叙

    #2.数据：
    #Atr_temp_long_4476x150 (4476,150)
    #AtrDivClose
    #AtrDivClose_true》0.055取真(4476,29)
    #统计AtrDivClose_true_sum
    3.分析：
    pct_chg/close 优势：pct_chg是当日涨幅，1.获取简单，不用变换，2.有正负号，没取绝对值
             劣势：不能表示当日波动情况，光头线会有比较好的表现。
    AtrDivClose/close：优势：能表示当日波动，真是的表现，十字星也能表现
                 劣势：有abs，高波动率可能是跌势，不能表示涨跌。只能说明振幅较大。
    解决办法：1.AtrDivClose_true矩阵(4476,29)与pct_chg>0矩阵取交集
            2.取交集的后4列

    :param num:
    :return:
    """
    def Atr_temp_long(num=30):
        df = Altas_db._readdf("ts", "2.his数据下载_fast1_long")

        n = list(set(df.trade_date))
        n.sort(reverse=True)

        if len(n)>= num:
            temp = df[df.trade_date == n[0]][['ts_code', "high", "low", "close", "vol", "pct_chg"]]
            for i in range(1,num):

                _df = df[df.trade_date == n[i]][['ts_code', "high", "low", "close", "vol", "pct_chg"]]
                temp = pd.merge(temp, _df, how='inner', on='ts_code')
            assert temp.shape[1] == 151
            temp.columns = [str(i) for i in range(5 * num + 1)]
            temp=Altas_db._save_mongo_db(temp, "temp/atr", "Atr_temp_long_4476x150")#(4476,150)#时间近到远
            return temp

          # temp[temp["0"]=="000001.SZ"]
        else:
            print("数量不够30个呀")
            get_his_data_fast(model="long")  # 更新读取数据
            Atr_temp_long()

    # TR = MAX(MAX(HIGH - LOW, ABS(HIGH - REF(CLOSE, 1))), ABS(LOW - REF(CLOSE, 1)))
    def AtrDivClose(a,temp:pd.DataFrame,model="1var"):
        """

        :param a:
        :param temp:
        :param dtype:
        MTR_data:当日
        Atr_10_data:10日ATR
        ATR_MAX:当日与10日ATR取最大
        "all":全部

        model="1var"  MTR_data:当日

        :return:
        """
        list_sum = []
        for i in range(0, num - 1):
            a1 = (a[:, 5 * i + 1] - a[:, 5 * i + 2])
            a2 = abs(a[:, 5 * i + 1] - a[:, 5 * i + 8])
            a3 = np.max([a1, a2], axis=0)
            a4 = abs(a[:, 5 * i + 2] - a[:, 5 * i + 8])
            a5 = np.max([a3, a4], axis=0)
            list_sum.append(a5)
            #print(i, 5 * i + 1, 5 * i + 2, 5 * i + 8, a5)
        MTR_data = pd.DataFrame(list_sum)[::-1]#（30，4476）-》从上到下
        """
        0 1 2 7 [0.28999999999999915]
        1 6 7 12 [0.4399999999999977]
        2 11 12 17 [0.3999999999999986]
        3 16 17 22 [0.3299999999999983]
        """

        if model == "1var":
            ATR_MAX=np.array(MTR_data)
        if model == "2var":
            Atr_10_data = MTR_data.rolling(10).mean()
            """
            21    NaN
            20    NaN
            19  0.595
            18  0.575
            17  0.554
            16  0.608
            15  0.618
            14  0.589
            """
            ATR_MAX = np.maximum(np.array(MTR_data), np.array(Atr_10_data))
        #print(ATR_MAX.shape)#(29, 4477)
        # 1.矩阵取最小值minimum（）
        # 2.矩阵相除F./E
        # print([data_atr_10,data])
        temp_close=temp[[str(5*i+3) for i in range(29)]].T[::-1]#(4470,29)->(29,4470) 倒叙
        #TRX=max(TR,ATR)/CLOSE>0.035 or max(TR[1],ATR[1])/CLOSE[1]>0.035 or max(TR[2],ATR[2])/CLOSE[2]>0.035
        # COUNT(TR/CLOSE>0.06,19)>=2
        AtrDivClose=(ATR_MAX/temp_close).T#（4470，29）
        if AtrDivClose.shape[0] == len(temp["0"]):  # （4470，29）
            AtrDivClose.index = temp["0"]

            # e_df=df_=pd.DataFrame({"data_his":e,"ts_code":temp["0"]})
            Altas_db._save_mongo_db(AtrDivClose, "temp/atr", "AtrDivClose_4476x29")
            return AtrDivClose
        else:
            print("数据维数错误，e表{}，code index{},请检查数据".format(AtrDivClose.shape[0], temp["0"]))
        if  model == "1var":
            return MTR_data,AtrDivClose
        if model == "2var":
            return MTR_data,Atr_10_data,ATR_MAX,AtrDivClose
    def AtrDivClose_trueASum(AtrDivClose,dtype="nodel"):
        """


        :param AtrDivClose:
        :param dtype:
        nodel:true_biao
        true_biao_sum:true_biao_sum
        all:
        :return:
        """
        # 1.取大于0.06
        # 2.行取整数
        # 3.增加code选项
        # 4.保存
        AtrDivClose_true=AtrDivClose>0.055
        true_biao=Altas_db._save_mongo_db(AtrDivClose_true.iloc[:, :], "temp/atr", "AtrDivClose_true")
        AtrDivClose_true_sum=AtrDivClose_true.sum(axis=1)
        df_=pd.DataFrame({"atr_19_true_day":AtrDivClose_true_sum})
        df=df_[df_.atr_19_true_day>=2]
        true_biao_sum=Altas_db._save_mongo_db(df, "temp/atr", "AtrDivClose_true_sum")
        if dtype=="nodel":
            return true_biao
        if dtype=="true_biao_sum":
            return true_biao_sum
        if dtype == "all":
            return true_biao,true_biao_sum
    def temp_pct(temp):
        temp_pct = temp[[str(5 * i + 5) for i in range(29)]].T[::-1]  # (4470,29)->(29,4470) 倒叙
        temp_pct=temp_pct.T
        #temp_pct= Altas_db._save_mongo_db(temp_pct, "temp/atr", "Atr_temp_pct")
        temp_pct_true= temp_pct > 0
        temp_pct_true.index = temp["0"]
        temp_pct_true = Altas_db._save_mongo_db(temp_pct_true.iloc[:, :], "temp/atr", "Atr_temp_pct_true")
        return temp_pct_true
    def _main():
        get_his_data_fast(model="long")#更新读取数据
        temp_df = Atr_temp_long()
        a = np.array(temp_df)
        AtrDivClose_df=AtrDivClose(a,temp_df)
        AtrDivClose_true_df=AtrDivClose_trueASum(AtrDivClose_df)
        temp_pct_true_df=temp_pct(temp_df)
        df_a=AtrDivClose_true_df.iloc[:,-4:]
        df_b=temp_pct_true_df.iloc[:,-4:]
        if df_a.shape==df_b.shape:
            atrApct_true=np.minimum(df_a,df_b)
            atrApct_true = Altas_db._save_mongo_db(atrApct_true, "temp/atr", "Atr_atrApct_true")
        atrApct_true_sum = atrApct_true.sum(axis=1)
        df_ = pd.DataFrame({"atrApct": atrApct_true_sum})
        df = df_[df_.atrApct>= 2]
        true_biao_sum = Altas_db._save_mongo_db(df, "temp/atr", "Atr_atrApct_true_sum")
        return atrApct_true
    _main()
def xixi():
    df_=Altas_db._read_ths_volMax()
    print(df_)
    pass

if __name__ == '__main__':
    #get_today_ts()
    #get_today_data_else()
    #get_his_data_fast()
    # # getHisData_main()
    # # df2Mergedf1()
    # # DMI_method_61()
    # # his_fast1_mdTOma5()
    DmiWith_V_ma5()
    import os
    import sys
    import pandas as pd
    pd.set_option('display.max_columns', None)
    pd.set_option('display.max_rows', None)
    #Atr_get_his_fast()
    xixi()
